package cn.iocoder.yudao.module.handguard.controller.app.houserecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.app.houserecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.houserecord.HouseRecordDO;
import cn.iocoder.yudao.module.handguard.convert.houserecord.HouseRecordConvert;
import cn.iocoder.yudao.module.handguard.service.houserecord.HouseRecordService;

@Tag(name = "管理后台 - 用户-房子资产记录")
@RestController
@RequestMapping("/hutai/house-record")
@Validated
public class HouseRecordController {

    @Resource
    private HouseRecordService houseRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户-房子资产记录")
    @PreAuthorize("@ss.hasPermission('hutai:house-record:create')")
    public CommonResult<Long> createHouseRecord(@Valid @RequestBody HouseRecordCreateReqVO createReqVO) {
        return success(houseRecordService.createHouseRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户-房子资产记录")
    @PreAuthorize("@ss.hasPermission('hutai:house-record:update')")
    public CommonResult<Boolean> updateHouseRecord(@Valid @RequestBody HouseRecordUpdateReqVO updateReqVO) {
        houseRecordService.updateHouseRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户-房子资产记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:house-record:delete')")
    public CommonResult<Boolean> deleteHouseRecord(@RequestParam("id") Long id) {
        houseRecordService.deleteHouseRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户-房子资产记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:house-record:query')")
    public CommonResult<HouseRecordRespVO> getHouseRecord(@RequestParam("id") Long id) {
        HouseRecordDO houseRecord = houseRecordService.getHouseRecord(id);
        return success(HouseRecordConvert.INSTANCE.convert(houseRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户-房子资产记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:house-record:query')")
    public CommonResult<List<HouseRecordRespVO>> getHouseRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<HouseRecordDO> list = houseRecordService.getHouseRecordList(ids);
        return success(HouseRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户-房子资产记录分页")
    @PreAuthorize("@ss.hasPermission('hutai:house-record:query')")
    public CommonResult<PageResult<HouseRecordRespVO>> getHouseRecordPage(@Valid HouseRecordPageReqVO pageVO) {
        PageResult<HouseRecordDO> pageResult = houseRecordService.getHouseRecordPage(pageVO);
        return success(HouseRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户-房子资产记录 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:house-record:export')")
    @OperateLog(type = EXPORT)
    public void exportHouseRecordExcel(@Valid HouseRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HouseRecordDO> list = houseRecordService.getHouseRecordList(exportReqVO);
        // 导出 Excel
        List<HouseRecordExcelVO> datas = HouseRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户-房子资产记录.xls", "数据", HouseRecordExcelVO.class, datas);
    }

}
